package com.example.jinmingwu.a180426;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv=new TextView(this);
        tv.setText("上下文菜单的载体");
        registerForContextMenu(tv);
        setContentView(tv);
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        menu.add(0,1,1,R.string.newfile);   //组别、ID、顺序、文本
        menu.add(0,1,1,R.string.open);
        menu.add(0,3,3,R.string.close);
        super.onCreateContextMenu(menu,v,menuInfo);
    }

    public boolean onContextItemSelected(MenuItem item){
        switch(item.getItemId()){
            case 1:
                Toast.makeText(this,"你点击了新建文件", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this,"你点击了打开", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                finish();
                break;
            default:
        }
        return true;
    }


//    public boolean onCreateOptionsMenu(Menu menu){
//        getMenuInflater().inflate(R.menu.main,menu);
//        return true;
//    }
//

     public boolean onCreateOptionsMenu(Menu menu) {
         menu.add(0,1,1,R.string.add);
         menu.add(0,2,2,R.string.remove);
         menu.add(0,6,6,R.string.close);

         SubMenu sub=menu.addSubMenu(R.string.title);
         sub.setIcon(android.R.drawable.ic_menu_search);
         sub.add(0,4,4,R.string.local);
         sub.add(0,5,5,R.string.network);
         sub.add(0,6,6,R.string.close);
         return true;
     }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.add_item:
            case 1:
                Toast.makeText(this,"你点击了add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove:
            case 2:
                Toast.makeText(this,"你点击了remove",Toast.LENGTH_SHORT).show();
                break;
            case R.id.exit_item:
            case 6:
                finish();
            case 4:
                Toast.makeText(this,"你点击了子菜单“本地”",Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(this,"你点击了子菜单“网络”",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
